<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests\Admin\AdminUserRequest;
use App\Models\AdminUser;
use App\Models\Role;
use App\Models\Auth;


class AdminUserController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, AdminUser $manager)
    {
        $admin_users = $manager->filter($request->all())->orderBy('id', 'desc')->paginate(config('api.page.limit'));
        $name = $request->name;
        $phone = $request->phone;
        return view('admins.admin.index',compact('admin_users','name', 'phone'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admins.admin.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AdminUserRequest $request, AdminUser $manager)
    {
        $result = $manager->create($request->all());
        if(!$result){
            return back()->with('warning','添加失败');
        }
        return redirect()->route('admin.managers.index')->with('success', '添加成功');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(AdminUser $manager)
    {
        return view('admins.admin.edit', compact('manager'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(AdminUserRequest $request, AdminUser $manager)
    {
        $result = $manager->update($request->all());
        if(!$result){
            return back()->with('warning','编辑失败');
        }
        return redirect()->route('admin.managers.index')->with('success', '编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete(Request $request, Auth $auth, AdminUser $admin)
    {
        $manager = $auth->where('parent_id', $request->id)->first();
        $admin = $admin->find($request->id);
        if($manager){
            return json_encode(['code' => 400, 'message' => '该账号下存在下级账户,请移除后再删除']);
        }
        $admin->delete();
        return json_encode(['code' => 200, 'message' => '删除成功']);
    }
    /**
     * 分配角色
     */
    
    public function setRole(AdminUser $manager)
    {
        return view('admins.admin.setroles', compact('manager'));
    }
    /**
     * [分配角色 description]
     * @param Request    $request    [description]
     * @param Role       $role       [description]
     * @param Permission $permission [description]
     */
    public function setRoleStore(Request $request, Role $role, Auth $manager)
    {   
        $manager_id = $request->manager_id;
        $manager = $manager->with('roles')->find($manager_id);
        $roles = $request->roles;
        if(!$roles){
            optional($manager)->roles()->detach();   
        }else{
            $res =  optional($manager)->assignRole(explode(',', $roles)); 
        }               
        return redirect()->route('admin.managers.index')->with('success', '编辑成功');
    }

    public function getRoles(Request $request, Role $role, Auth $manager)
    {
        $manager_id = $request->manager_id;
        $manager = $manager->with('roles')->find($manager_id);
        $roles = $role->get();
        $manager_roles = optional($manager)->roles;        
        $manager_roles = $manager_roles->pluck('id');
        return json_encode(['code' => 200, 'msg' => 'success', 'data' => compact('manager_roles','roles')]);
    }
}
